﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FormControllers;
using AgencyEntityLib.BaseEntity;
using Ecopsy.Tools;
using SqlLibrary;
using Ecopsy.DataTier.SqlLibrary;

public partial class iFrameAdmin_Groups : iAdmin_Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack )MyDataBind();
    }

    private void MyDataBind() 
    {
        ObjectDS_PF ODS = new ObjectDS_PF(GetList);
        lvSurfaceGroups .DataSource = ODS.ImAsObjectDataSource;
        DataBind();      

    }
    
    protected void AddButton_Click(object sender, EventArgs e)
    {
        string s = "iFrameAdmin_EditSurfaceGroup.aspx?" + qSurfaceGroupID + "=" + GlobalConstants.qPrefNEW_RECORD + Guid.NewGuid().ToString();        
        Response.Redirect(s);
    }
    
    protected override ListWhere Where
    {
        get
        {
            return f_Where (new SuraceGroupList.sqlLoadStatment(),"Name");
        }
    }


    public SurfaceGroup[] GetList(int startRowIndex, int maximumRows, out int RowCount)
    {

        wsAgency _ws = new wsAgency();
        ListWhere w = (ListWhere)Where;
        w.ListLoadStatment.StartRowIndex = startRowIndex;
        w.ListLoadStatment.EndRowIndex = startRowIndex + maximumRows;
        SuraceGroupList l = _ws.GetSurfaceGroupList((SuraceGroupList.sqlLoadStatment)w.ListLoadStatment);
        RowCount = l.OutputRwCnt;

        //заряжаем comparer функцией сортировки 
        Where.SortCondition.fnOrderBy = SortFunctions.fOrderBy;
        // Сортируем
        SurfaceGroup[] r = l.ToArray<SurfaceGroup>();
        Array.Sort(r, Where.SortCondition);
        return r;

    }

    protected void lvSort(object sender, EventArgs e)
    {
        
        f_lvSort(sender, lvSurfaceGroups, MyDataBind); 
    }

    protected void lvSurfaceGroups_PagePropertiesChanged(object sender, EventArgs e)
    {
        MyDataBind();
    }

    protected void OnDeleteClick(object sender, EventArgs e)
    {
        ImageButton btn = (ImageButton)sender;
        GlobalConstants.CurrentAgency.SurfaceGroups.Remove(btn.CommandArgument);
        
        wsAgency ws = new wsAgency();
        ws.SaveAgency();        
        
        MyDataBind();

    }

    protected void Find_Click(object sender, ImageClickEventArgs e)
    {
        string sFlt = txtSearch.Text;
        int errPosition = -1;
        if (!ExpressionChecker.CheckStatment(ref sFlt, ref errPosition))
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "AlertBadFiltr", "alert('Ошибочное выражение фильтрации');", true);
            return;
        }

        ((SuraceGroupList.sqlLoadStatment)Where.ListLoadStatment).Lexeme = sFlt;
        MyDataBind ();
    }

    protected string GetEditCommand(string ID)
    {        
        //string s ="iFrameAdmin_Group_EditSurfaceGroup.aspx?" + qSurfaceGroupID + "="+ ID + "" ;
        //return "alert('zxzxzx'); document.location = 'iFrameAdmin_Group_EditSurfaceGroup.aspx'";
        return "Redirect('" + ID + "');";
    }
}
